package ccgl.com.server;

import java.sql.SQLException;
import java.util.Map;

import ccgl.com.dao.KhglDao;
import ccgl.com.model.MKh;
import ccgl.com.model.MKhSearchItem;
import ccgl.com.util.UUIDUitl;

/**
 * 货物管理服务接口
 * 
 * @author Administrator
 * 
 */
public class KhglServer {

	KhglDao dao = new KhglDao();

	/**
	 * 添加
	 * @param kh
	 * @return
	 * @throws SQLException 
	 */
	public String addKhxx(MKh kh) throws SQLException {
		kh.setId(UUIDUitl.getNewId());
		if (dao.addKhxx(kh)) {
			return "保存成功";
		}
		return "保存出错！";
	}

	public String updateKhxx(MKh kh) throws SQLException {
		if (dao.updateKhxx(kh)) {
			return "保存成功";
		}
		return "保存出错！";
	}

	
	public Map getKhList(int star, int end, String userid) throws SQLException {
		return dao.getKhList(star, end, userid);
	}
	
	public Map getKhListWithParams(MKhSearchItem item, int star, int end, String userid) throws SQLException {
		String sql = "SELECT * FROM khb t where userid='" + userid + "'";
		String countSql = "select count(*) from khb t where userid='" + userid + "'";
		
		if(item.getXm() != null) {
			sql += " and t.xm like '%" + item.getXm() + "%'";
			countSql += " and t.xm like '%" + item.getXm() + "%'";
		}
		
		sql +=" LIMIT " + star + "," + end;
		
		return dao.getKhList(sql, countSql);
	}

	public boolean deleteKhxxs(String ids) throws SQLException {
		String[] idArr = ids.split("-");
		String idStr = "";
		for(int i =0;i<idArr.length;i++){
			if(i==(idArr.length-1) ){
				idStr += "'" + idArr[i] + "'";
			}else{
				idStr += "'" + idArr[i] + "',";
			}
		}
		return dao.deleteKhxxs(idStr);
	}
}
